package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SServiceOrderStatusLogService;
import cn.huiyunche.base.service.mappers.SServiceOrderStatusLogMapper;
import cn.huiyunche.base.service.mappers.ext.SServiceOrderStatusLogExtMapper;
import cn.huiyunche.base.service.model.SServiceOrderStatusLog;
import cn.huiyunche.base.service.model.SServiceOrderStatusLogExample;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class SServiceOrderStatusLogServiceImpl implements SServiceOrderStatusLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SServiceOrderStatusLogServiceImpl.class);

    @Autowired
    private SServiceOrderStatusLogMapper sServiceOrderStatusLogMapper;

    @Autowired
    private SServiceOrderStatusLogExtMapper sServiceOrderStatusLogExtMapper;

    @Override
    public void batchAdd(Collection<SServiceOrderStatusLog> collection) {
        LOGGER.info("batch insert s service order log params collection : {}.", collection);
        try {
            sServiceOrderStatusLogExtMapper.insertBatch(collection);
        } catch (Exception e) {
            LOGGER.error("batch insert s service order log params collection : {}, error : {}.", collection, e);
            throw new BusinessException("批量添加服务订单日志异常");
        }
    }

    @Override
    public int addLog(SServiceOrderStatusLog serviceOrderStatusLog) {
        LOGGER.info("insert s service order log params serviceOrderStatusLog : {}.", serviceOrderStatusLog);
        try {
            return sServiceOrderStatusLogMapper.insertSelective(serviceOrderStatusLog);
        } catch (Exception e) {
            LOGGER.error("insert s service order log params serviceOrderStatusLog : {}, error : {}.", serviceOrderStatusLog, e);
            throw new BusinessException("服务订单添加日志异常");
        }
    }

    @Override
    public int delete(Long serviceOrderId) {
        LOGGER.info("delete param : {}", serviceOrderId);
        if (null == serviceOrderId) {
            LOGGER.error("delete param must not be null");
            throw new IllegalArgumentException("服务订单id不能为空");
        }

        SServiceOrderStatusLogExample example = new SServiceOrderStatusLogExample();
        example.createCriteria().andServiceOrderIdEqualTo(serviceOrderId);
        int count = sServiceOrderStatusLogMapper.deleteByExample(example);

        return count;
    }

}
